<?php
/**
 * created by zzy
 * date: 2018/1/16 10:48
 */
namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;

class BillTemplate extends DbEloquent
{
    use SoftDeletes;
    protected $dates    = ['deleted_at'];
    protected $table    = 'ygt_bill_template';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'登录用户异常','value'=>0,'is_must'=>1],
        ['name'=>'type','title'=>'类型','value'=>0,'is_must'=>0,'type'=>1],
        ['name'=>'title','title'=>'名称','value'=>'','is_must'=>1],
        ['name'=>'bill_category_id','title'=>'分类','value'=>0,'is_must'=>1,'type'=>1],
        ['name'=>'bill_account_id','title'=>'账户','value'=>0,'is_must'=>1,'type'=>1],
        ['name'=>'bill_buyer_id','title'=>'往来单位','value'=>0,'is_must'=>1,'type'=>1],
        ['name'=>'money','title'=>'金额','value'=>0,'is_must'=>1],
        ['name'=>'remark','title'=>'备注','value'=>'','is_must'=>0],
    ];
    //取type的配置列表,,,,,,,写在配置文件更好???待定
    public static function getBillTypeList()
    {
        $result             = [
            ['id'=>1,'title'=>'支出模板'],['id'=>2,'title'=>'收入模板'],
        ];
        return $result;
    }
    public static function getListDetailByCompanyId($companyId,$limit,$offset)
    {
        $where              = ['ygt_bill_template.company_id'=>$companyId];
        $result             = self::getListDetail($where,$limit,$offset);
        return $result;
    }
    public static function getInfoDetail($companyId,$id)
    {
        $where              = [
            'ygt_bill_template.company_id'=>$companyId,
            'ygt_bill_template.id'=>$id
        ];
        $limit              = $offset = '';
        $isList             = false;
        $result             = self::getListDetail($where,$limit,$offset,$isList);
        return $result;
    }
    //取模板列表
    public static function getListDetail($where,$limit,$offset,$isList=true)
    {
        $columns            = 'ygt_bill_template.id,ygt_bill_template.type,ygt_bill_template.title';
        $columns            .= ',ygt_bill_template.bill_category_id,ygt_bill_template.bill_account_id';
        $columns            .= ',ygt_bill_template.bill_buyer_id,ygt_bill_template.money';
        $columns            .= ',ygt_bill_template.img_id,ygt_bill_template.remark';
        $columns            .= ',ygt_bill_category.title as bill_category_name';
        $columns            .= ',ygt_bill_account.title as bill_account_name';
        $columns            .= ',ygt_bill_buyer.title as bill_buyer_name';
        $join               = [
            ['table'=>'ygt_bill_category','field_l'=>'ygt_bill_category.id','field_c'=>'=','field_r'=>'ygt_bill_template.bill_category_id'],
            ['table'=>'ygt_bill_account','field_l'=>'ygt_bill_account.id','field_c'=>'=','field_r'=>'ygt_bill_template.bill_account_id'],
            ['table'=>'ygt_bill_buyer','field_l'=>'ygt_bill_buyer.id','field_c'=>'=','field_r'=>'ygt_bill_template.bill_buyer_id'],
        ];
        $orderby            = ['ygt_bill_template.id','desc'];
        $groupby            = '';
        if($isList){
            $lists          = self::getList($where,$columns,$limit,$offset,$orderby,$groupby,$join)->toArray();
            foreach($lists as $key=>$val)
            {
                $text                   = '示例模板';
                $lists[$key]['text']    = $text;
            }
            $result             = $lists;
        }else{
            $result             = self::getInfo($where,$columns,$orderby,$groupby,$join);
        }
        return $result;
    }

}
